from XEdu.hub import Workflow as wf
import numpy as np
import cv2
flag = 0
text = ' '
body = wf(task='body26' )
basenn = wf(task='basenn',checkpoint='model/20240522.onnx')
cap = cv2.VideoCapture(0)
while True:
    ret,img = cap.read()
    if ret:    
        result = body.inference(data=img)
        result = np.concatenate(result).reshape(1, -1)#将推理结果重整为一行的一维数组
        res = basenn.inference(data=result)
        res_show = basenn.format_output(lang="en")
        label = ['tuobei','ok']
        num = res_show[0]["prediction"]#rea_show是一个二维数组，其中第一的值为一个字典
        
        position = (50, 100) # 左上角起始点的坐标
        font = cv2.FONT_HERSHEY_SIMPLEX
        scale = 1
        color = (255, 255, 255) # BGR颜色值（白色）
        thickness = 2
 
        # 将文字写入图像中
        if num == num:#手势不变，计数加一
            flag = flag+1
        else:
            flag = 0#否则清零
        if flag >=30:#大于30，输出
            print(num)
            flag = 0
            text = label[num]#文本内容
        cv2.putText(img, text, position, font, scale, color, thickness)
        
        
        # 显示结果图像
        cv2.imshow("Image with Text", img)
        if cv2.waitKey(1) & 0xFF == 27:
            break
cap.release()
cv2.destroyAllWindows